package com.handinfo.android.uicontrols.controls;

import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.utils.Tools;

/**
 * 子类控件
 * @author handinfo
 */
public class DWListSubItem extends DWControl
{
	private boolean m_is_auto = true;//是不是需要自动计算宽高
	private CopyOnWriteArrayList<DWControl> m_controls = new CopyOnWriteArrayList<DWControl>();
	public DWListSubItem()
	{
		
	}

	public void render(DWGraphics g)
	{

	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		g.setColor(Tools.WHITE);
		DWControl control = null;
		for (int i = 0; i < m_controls.size(); i++) {
			control = m_controls.get(i);
			control.renderLimits(g, x, y, w, h, offsetx, offsety);
		}
		renderLimitsRect(g, x, y, offsetx, offsety);
		renderGuide(g, x, y);
	}

	public void logic()
	{
		for (int i = 0; i < m_controls.size(); i++) {
			m_controls.get(i).logic();
		}
	}

	public void addControls(DWControl control)
	{
		if (control != null) {
			m_controls.add(control);
			refreshControl();
		}
	}
	
	/**
	 * 设置是否需要自动刷新宽高
	 * @param auto
	 */
	public void setAutoWideHigh(boolean auto) {
		m_is_auto = auto;
	}

	public void refreshControl()
	{
		DWControl control = null;
		int minX = 0;
		int minY = 0;
		int maxW = 0;
		int maxH = 0;
		for (int i = 0; i < m_controls.size(); i++) {
			control = m_controls.get(i);
			control.setDefaultNearAnchorUn();
		}
		if (m_is_auto) {
			for (int i = 0; i < m_controls.size(); i++) {
				control = m_controls.get(i);
				if (control.m_show_x < minX || i == 0) {
					minX = control.m_show_x;
				}
				if (control.m_show_y < minY || i == 0) {
					minY = control.m_show_y;
				}
				if (control.m_show_x + control.m_show_w > maxW) {
					maxW = control.m_show_x + control.m_show_w;
				}
				if (control.m_show_y + control.m_show_h > maxH) {
					maxH = control.m_show_y + control.m_show_h;
				}
			}
			m_show_w = maxW - minX;
			m_show_h = maxH - minY;
			if (m_show_h < 30) {
				m_show_h = 30;
			}
		}
	}

	/**
	 * 得到子类控件中的控件
	 * @param index
	 * @return
	 */
	public DWControl getControl(int index) {
		if (index > -1 && index < m_controls.size()) {
			return m_controls.get(index);
		}
		return null;
	}
	/**
	 * 移除一个控件
	 * @param index 下标
	 */
	public DWControl removeControl(int index) {
		if (index > -1 && index < m_controls.size()) {
			DWControl remove = m_controls.remove(index);
			if (remove != null) {
				refreshControl();
			}
			return remove;
		}
		return null;
	}
	/**
	 * 移除一个控件
	 * @param control 控件
	 */
	public boolean removeControl(DWControl control) {
		if (control != null && m_controls.contains(control)) {
			m_controls.remove(control);
			refreshControl();
			return true;
		}
		return false;
	}
	
	/**
	 * 查找当前子项内是否包含此控件
	 * @param control
	 * @return
	 */
	public boolean contains(DWControl control) {
		if (control != null) {
			return m_controls.contains(control);
		}
		return false;
	}
	
//	public void updateSubItemWide() {
//		int w = m_show_w;
//		DWControl tb = null;
//		for (int i = 0; i < m_controls.size(); i++) {
//			if (m_controls.get(i) instanceof DWTextbox) {
//				tb = m_controls.get(i);
//			} else {
//				w -= m_controls.get(i).getShowWidth();
//			}
//		}
//		if (tb != null && w > 0) {
//			tb.setShowWideHigh(w, tb.m_show_h);
//		}
//	}
	
	public boolean doLongPress(PointF point) {
		for (int i = m_controls.size() - 1; i > -1; i--) {
			if (m_controls.get(i).doLongPress(point)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean doDown(PointF point)
	{
		for (int i = m_controls.size() - 1; i > -1; i--) {
			if (m_controls.get(i).doDown(point)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		for (int i = m_controls.size() - 1; i > -1; i--) {
			if (m_controls.get(i).doDrag(start, end, velocityX, velocityY)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		for (int i = m_controls.size() - 1; i > -1; i--) {
			if (m_controls.get(i).doFling(start, end, velocityX, velocityY)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(start.x, start.y)) {
			DWControl control = null;
			for (int i = m_controls.size() - 1; i > -1; i--) {
				control = m_controls.get(i);
				if (control.doScroll(start, end, distanceX, distanceY)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			setGuide(false);
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doClick(x, y)) {
					return true;
				}
			}
		}
		return false;
	}

}
